import java.util.Calendar;

import it.trekker.dao.DAOFactory;
import it.trekker.dao.Excursion;
import it.trekker.dao.ExcursionCoordinate;
import it.trekker.dao.GPSCoordinate;
import it.trekker.dao.IExcursionDAO;
import it.trekker.dao.IExcursionCoordinateDAO;
import it.trekker.dao.IPoiDAO;
import it.trekker.dao.IUserDAO;
import it.trekker.dao.Poi;
import it.trekker.dao.User;
import it.trekker.dao.ejb.EJB3DAOFactory;


public class initializeTables {

	protected DAOFactory daoFactory = DAOFactory.getDAOFactory(EJB3DAOFactory.class.getName());
	protected IUserDAO userDAO = daoFactory.getUserDAO();
	protected IExcursionDAO excursionDAO = daoFactory.getExcursionDAO();
	protected IExcursionCoordinateDAO excursionCoordinateDAO = daoFactory.getExcursionCoordinateDAO();
	protected IPoiDAO poiDAO = daoFactory.getPoiDAO(); 
	
	/**
	 * @param args
	 */
	public static void main(String[] args) {
		initializeTables it = new initializeTables();
		it.init();
	}
	
	
	
	protected void init() {
		Excursion excursion;
		User user;
		
		// Excursion 1
		
		user = insertAUser("Pippo");
		excursion = insertAnExcursion(user, "Casa", 3200, 12.3212, 7200000, 10);
		
		insertACoordinate(excursion, 45.2239, 10.7507, 88);
		insertACoordinate(excursion, 45.2339, 10.7407, 88);
		insertACoordinate(excursion, 45.2439, 10.7407, 88);
		insertACoordinate(excursion, 45.2339, 10.7207, 88);
		insertACoordinate(excursion, 45.2299, 10.7257, 88);
		
		insertAPoi(excursion, "Poi1", 45.2339, 10.7407);
		insertAPoi(excursion, "Poi2", 45.2339, 10.7207);
		
		// Excursion 2
		
		user = insertAUser("Pluto");
		excursion = insertAnExcursion(user, "Prova", 4250, 10.5234, 8200000, 12);
		
		insertACoordinate(excursion, 45.0239, 10.5507, 88);
		insertACoordinate(excursion, 45.0339, 10.5407, 88);
		insertACoordinate(excursion, 45.0439, 10.5407, 88);
		insertACoordinate(excursion, 45.0339, 10.5207, 88);
		insertACoordinate(excursion, 45.0299, 10.5257, 88);
		
		insertAPoi(excursion, "Poi1", 45.0339, 10.5407);
		insertAPoi(excursion, "Poi2", 45.0339, 10.5207);
	}
	
	
	
	protected User insertAUser(String username) {
		User user = new User(username);
		user.setId(userDAO.insert(user));
		
		return user;
	}
	
	protected Excursion insertAnExcursion(User user, String name, double distance, double meanPace, int time, double totAltitudeDiff) {
		Excursion excursion = new Excursion();
		excursion.setUser(user);
		excursion.setDate(Calendar.getInstance().getTime());
		excursion.setName(name);
		excursion.setSteps(0);
		excursion.setDistance(distance);
		excursion.setMeanPace(meanPace);
		excursion.setTime(time);
		excursion.setTotAltitudeDifference(totAltitudeDiff);
		excursion.setId(excursionDAO.insert(excursion));
		
		return excursion;
	}
	
	protected ExcursionCoordinate insertACoordinate(Excursion excursion, double latitude, double longitude, double altitude) {
		ExcursionCoordinate coord = new ExcursionCoordinate();
		coord.setExcursion(excursion);
		coord.getCoordinate().setLatitude(latitude);
		coord.getCoordinate().setLongitude(longitude);
		coord.getCoordinate().setAltitude(altitude);
		coord.setTimestamp(Calendar.getInstance().getTime());
		coord.setId(excursionCoordinateDAO.insert(coord));
		
		return coord;
	}
	
	protected Poi insertAPoi(Excursion excursion, String friendlyName, double latitude, double longitude) {
		GPSCoordinate location = new GPSCoordinate();
		location.setLatitude(latitude);
		location.setLongitude(longitude);
		
		Poi poi = new Poi();
		poi.setExcursion(excursion);
		poi.setFriendlyName(friendlyName);
		poi.setLocation(location);
		poi.setId(poiDAO.insert(poi));
		
		return poi;
	}
	
	
}
